package com.mofang.activity.login;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.mofang.R;
import com.mofang.activity.BaseTitleViewActiviey;
import com.mofang.activity.MofangCommonUpdateView;
import com.mofang.controller.UserController;
import com.mofang.widget.AlertDialog;
import com.pactera.framework.exception.IException;
import com.pactera.framework.util.ToastHelper;

public class RegisterActivity extends BaseTitleViewActiviey {

	private EditText edit_register_invitationCode;
	private EditText edit_register_realName;
	private EditText edit_register_phone;
	private EditText edit_register_captcha;
	private EditText edit_register_password;
	private EditText edit_register_again_Password;
	private CheckBox chk_login_user_agreement;
	private Button btn_register_captcha;
	private Button btn_register;
	private UserController userController;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_login_register);
		super.onCreate(savedInstanceState);
		setNavigationTitle("用户注册");
		setNavigationLeftBtnImage(R.drawable.return_arrow);
		userController = new UserController();
	}

	@Override
	protected void initView() {
		super.initView();
		edit_register_invitationCode = (EditText) findViewById(R.id.edit_register_invitationCode);
		edit_register_realName = (EditText) findViewById(R.id.edit_register_realName);
		edit_register_phone = (EditText) findViewById(R.id.edit_register_phone);
		edit_register_captcha = (EditText) findViewById(R.id.edit_register_captcha);
		edit_register_password = (EditText) findViewById(R.id.edit_register_password);
		chk_login_user_agreement = (CheckBox) findViewById(R.id.chk_login_user_agreement);
		btn_register_captcha = (Button) findViewById(R.id.btn_register_captcha);
		btn_register = (Button) findViewById(R.id.btn_register);
	}

	Handler handler = new Handler();

	@Override
	protected void setListener() {
		super.setListener();
		btn_register_captcha.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// finish();
				// 发送验证码
				String phoneNo = "";
				userController.sendVerificationCode(
						new VerificationCodeUpdateView(mContext), phoneNo);
			}
		});
		btn_register.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// if(validateInput()){
				// 注册
				String register_invitationCode = edit_register_invitationCode
						.getText().toString();
				String register_realName = edit_register_realName.getText()
						.toString();
				String register_phone = edit_register_phone.getText()
						.toString();
				String register_captcha = edit_register_captcha.getText()
						.toString();
				String register_password = edit_register_password.getText()
						.toString();
				userController.userRegister(
						new UserRegisterUpdateView(mContext),
						register_invitationCode, register_realName,
						register_phone, register_captcha, register_password);

			}
		});
	}

	private boolean validateInput() {
		String register_invitationCode = edit_register_invitationCode.getText()
				.toString();
		String register_realName = edit_register_realName.getText().toString();

		if (TextUtils.isEmpty(register_invitationCode)) {
			edit_register_invitationCode.requestFocus();
			ToastHelper.getInstance().showLongMsg(
					getString(R.string.login_account_not_null));
			return false;
		}
		if (TextUtils.isEmpty(register_realName)) {
			edit_register_realName.requestFocus();
			ToastHelper.getInstance().showLongMsg(
					getString(R.string.login_password_not_null));
			return false;
		}
		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.register, menu);
		return true;
	}

	private class VerificationCodeUpdateView extends
			MofangCommonUpdateView<Boolean> {
		private ProgressDialog progressDialog;

		public VerificationCodeUpdateView(Context context) {
			super(context);
		}

		@Override
		public void onPreExecute() {
			super.onPreExecute();
			progressDialog = ProgressDialog.show(RegisterActivity.this, null,
					"正在发送验证码", true, false);
		}

		@Override
		public void onPostExecute(Boolean result) {
			// result=false;
			progressDialog.dismiss();
			if (result) {
				ToastHelper.getInstance().showLongMsg("发送成功");
			} else {
				viewHelper.showBTNDialog("发送失败，请重试");
			}
		}

		@Override
		public void handleException(IException ex) {
			viewHelper.showBTNDialog(ex.getMessage());
		}
	}

	private class UserRegisterUpdateView extends
			MofangCommonUpdateView<Boolean> {
		private ProgressDialog progressDialog;

		public UserRegisterUpdateView(Context context) {
			super(context);
		}

		@Override
		public void onPreExecute() {
			super.onPreExecute();
			progressDialog = ProgressDialog.show(RegisterActivity.this, null,
					"正在注册", true, false);
		}

		@Override
		public void onPostExecute(Boolean result) {
			// result=false;
			progressDialog.dismiss();
			if (result) {
				// ToastHelper.getInstance().showLongMsg(
				// getResources().getString(R.string.register_success));
				AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
				builder.setMessage(mContext.getResources().getString(
						R.string.register_success));
				builder.setImageResourceId(R.drawable.logo);
				final AlertDialog alertDialog = builder.create();
				alertDialog.show();
				handler.postDelayed(new Runnable() {

					@Override
					public void run() {
						alertDialog.dismiss();
					}
				}, 1000);
			} else {
				viewHelper.showBTNDialog(getResources().getString(
						R.string.register_fail));
			}
		}

		@Override
		public void handleException(IException ex) {
			viewHelper.showBTNDialog(ex.getMessage());
		}
	}
}
